<?php
namespace app\wechatxcx\controller;


use app\common\model\ChangguanYuyue;
use app\common\model\ChangguanChangdi;
use app\common\model\ChangguanFeiyong;
use app\common\model\ChangguanFenleiChangdi;
use app\common\model\ChangguanJinyong;
use think\Db;
use \app\common\model\Changguan as ChangguanModel;

use zjkal\ChinaHoliday;

class Yuding extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 分页列表
     * @return \think\response\Json
     */
    public function list()
    {
        //查询条件
        $where = array(
            'checked'=>input('checked'),
            'uid'=>$this->uid
        );

        //分页信息
        $page = max(input('page'), 1);
        $size = input('size',20);

        //获取分页列表
        $list = ChangguanYuyue::getGroupby($where, $page, $size, 'id desc','cgid,checked,status,create_time,order_sn,start_time,date,sum(price) as total','order_sn');
        //总页码
        $total = ChangguanYuyue::getGroupby($where);
        //数据返回
        if (!$list) {
            return returnJson(200, '', array('list' => [], 'total' => 0));
        }
        $fl_cd = ChangguanFenleiChangdi::getColumn([],'id,label');
        $cg = ChangguanModel::getColumn([],'id,name,phone');
        foreach ($list as &$v){
            $v['name'] = isset($cg[$v['cgid']])?$cg[$v['cgid']]['name']:'';
            $v['phone'] = isset($cg[$v['cgid']])?$cg[$v['cgid']]['phone']:'';
            $v['date'] = date('Y/m/d',strtotime($v['date']));
            $changdi = ChangguanYuyue::getAll(array('order_sn'=>$v['order_sn']),'start_time asc');
            foreach ($changdi as &$vv){
                $vv['cate'] = isset($fl_cd[$vv['flid']])?$fl_cd[$vv['flid']]:'';
                $vv['cd'] = isset($fl_cd[$vv['cdid']])?$fl_cd[$vv['cdid']]:'';
                $vv['date_formate'] = date('m/d',strtotime($vv['date']));
            }
            $v['changdi'] = $changdi;
        }

        return returnJson(200, '', [
            'list' => $list,
            'total' => $total
        ]);
    }
    /**
     * 创建订单
     * @return \think\response\Json
     */
    public function createorder()
    {

        $list = input('list/a');
        if($list){
            $save = [];
            $fenlei = ChangguanFenleiChangdi::getColumn([],'id,pid');
            $order_sn = create_order_sn($this->user['id'],'CG');
            foreach ($list as $v){
                $save[] = [
                    'cgid' => input('cgid'),
                    'cdid' => $v['cdid'],
                    'flid' => $fenlei[$v['cdid']],
                    'order_sn'=>$order_sn,
                    'create_time' =>time(),
                    'update_time' =>time(),
                    'price' => $v['price'],
                    'start_time' => $v['start_time'],
                    'end_time' => $v['end_time'],
                    'date' => input('date'),
                    'uid' => $this->user['id']
                ];
            }
            ChangguanYuyue::setSaveAll($save);
            return returnJson(200, 'success', $order_sn);
        }
        return returnJson(202, '订单生成失败');

    }

    /**
     * 场馆-订单详情
     * @return \think\response\Json
     */
    public function orderdetail()
    {
        $where = array(
            'order_sn'=>input('order_sn')
        );
        $data = ChangguanYuyue::getAll($where,'start_time asc');

        $changguan = ChangguanModel::getOne(array('id'=>$data[0]['cdid']));

        $fl_cd = ChangguanFenleiChangdi::getColumn([],'id,label');

        foreach ($data as &$v){
            $v['cate'] = isset($fl_cd[$v['flid']])?$fl_cd[$v['flid']]:'';
            $v['changdi'] = isset($fl_cd[$v['cdid']])?$fl_cd[$v['cdid']]:'';
            $v['date_formate'] = date('m/d',strtotime($v['date']));
        }

        return returnJson(200, 'success', compact('data','changguan'));
    }

    /**
     * 场馆-取消订单
     * @return \think\response\Json
     */
    public function cancelorder()
    {
        $where = array(
            'order_sn'=>input('order_sn')
        );
        $save = array(
            'checked'=>input('checked'),
            'status'=>input('status')
        );
        ChangguanYuyue::update($save,$where);
        return returnJson(200, 'success');
    }

    /**
     * 更新订单
     * @return \think\response\Json
     */
    public function updateorder()
    {
        $where = array(
            'order_sn'=>input('order_sn')
        );
        if(input('remark')){
            $save = [
                'remark' => input('remark'),
            ];
            ChangguanYuyue::update($save,$where);
        }
        return returnJson(200, 'success');
    }



}